package level.editor.menu;

import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;

import core.Constants;

import level.editor.command.Command;
import level.editor.state.UpdateMenuCommand;

/*
 * Implementation of edit menu category. Planned functions include: Cut, Copy,
 * Paste, Delete. Actual implementation will happen in StateModule.
 */

public class MenuEdit extends MenuCategory
{
	private JMenuItem undoButton;
	private JMenuItem redoButton;

	public MenuEdit(MenuView view)
	{
		super("Edit", view);
		setMnemonic(KeyEvent.VK_E);
		getAccessibleContext().setAccessibleDescription(
				"This is the main edit menu");
	}

	@Override
	protected void init()
	{
		String iconDir = Constants.ICON_DIR;
		
		JMenuItem undoButton = new JMenuItem("Undo", new ImageIcon(iconDir
				+ "/undo.png"));
		this.undoButton = undoButton;
		undoButton.setActionCommand("UNDO");
		undoButton.addActionListener(view);
		undoButton.setEnabled(false);
		undoButton.setMnemonic(KeyEvent.VK_U);
		add(undoButton);

		JMenuItem redoButton = new JMenuItem("Redo", new ImageIcon(iconDir
				+ "/redo.png"));
		this.redoButton = redoButton;
		redoButton.setActionCommand("REDO");
		redoButton.addActionListener(view);
		redoButton.setEnabled(false);
		redoButton.setMnemonic(KeyEvent.VK_R);
		add(redoButton);
	}

	@Override
	protected void pushDownCommand(Command command)
	{
		UpdateMenuCommand menuCommand = (UpdateMenuCommand) command;
		if (undoButton != null)
		{
			undoButton.setEnabled(!menuCommand.isPositionZero());
		}

		if (redoButton != null)
		{
			redoButton.setEnabled(!menuCommand.isPositionEnd());
		}
	}

}
